/**public static void main(String[] args) {
	List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
	for (int i = 0; i < 5; i++) {
		Map<String,Object> parentMap = new HashMap<String, Object>();
		parentMap.put("code", "p"+i);
		parentMap.put("name", "父级"+i);
		List<Map<String,Object>> childList = new ArrayList<Map<String,Object>>();
		for (int j = 0; j <3; j++) {
			Map<String,Object> childMap = new HashMap<String, Object>();
			childMap.put("code", "p"+i+"-c"+j);
			childMap.put("name", "父级"+i+"-子级"+j);
			childList.add(childMap);
		}
		parentMap.put("childs", childList);
		list.add(parentMap);
	}
	System.out.println(JsonDecoder.toJSON(list));
}*/
/**
 * BinJsonArray json数据 $BinParentNode 父级菜单节点 $BinChildNode 子级菜单节点 BinMap map对象
 * defaultP 父级缺醒 defaultC 子级缺醒
 */
function Bin() {
	return {
		BinJsonArray : null,
		$BinParentNode : null,
		$BinChildNode : null,
		BinMap : null,
		defaultP : '',
		defaultC : '',
		init : function(BinJsonArray, $BinParentNode, $BinChildNode, defaultP,
				defaultC) {
			this.BinJsonArray = BinJsonArray;
			this.$BinParentNode = $($BinParentNode);
			this.$BinChildNode = $($BinChildNode);
			this.defaultP = defaultP;
			this.defaultC = defaultC;
			this.BinMap = new this.initMap();
			this.initSelect();
			this.initBind(this);
			return this;
		},
		initBind : function(IBin) {
			this.$BinParentNode.bind('change', function() {
				IBin.initChild(this.value);
			});
		},
		initSelect : function() {
			this.$BinParentNode.empty();
			this.$BinParentNode.append('<option value="">' + this.defaultP+ '</option>');

			if (typeof (this.BinJsonArray) == "undefined") {

			} else {
				if (typeof (this.BinMap) == "undefined") {
					this.BinMap = new this.initMap();
				}
				if (this.BinJsonArray != null) {
					var BinParentLen = this.BinJsonArray.length;
					for (var i = 0; i < BinParentLen; i++) {
						this.$BinParentNode.append('<option value="'+ this.BinJsonArray[i].code + '">'+ this.BinJsonArray[i].name + '</option>');
						this.BinMap.put(this.BinJsonArray[i].code,this.BinJsonArray[i].childs);
					}

				}
			}
			this.initChild();
		},
		initChild : function(parentCode) {

			this.$BinChildNode.empty();
			this.$BinChildNode.append('<option value="">' + this.defaultC+ '</option>');
			if (!(typeof (parentCode) == "undefined") || !parentCode == null) {
				var ChildArray = this.BinMap.get(parentCode);
				if (typeof (ChildArray) == "undefined") {

				} else {
					if (ChildArray != null) {
						var BinChildLen = ChildArray.length;
						for (var i = 0; i < BinChildLen; i++) {
							this.$BinChildNode.append('<option value="'+ ChildArray[i].code + '">'+ ChildArray[i].name + '</option>');

						}

					}
				}
			}
		},
		IArray:null,
		$IOne:null,
		$ITwo:null,
		$IThree:null,
		defaultOne:'',
		defaultTwo:'',
		defaultThree:'',
		defaultOneVal:'',
		defaultTwoVal:'',
		defaultThreeVal:'',
		oneMap:null,
		twoMap:null,
		init2 : function(IArray, $IOne,defaultOne,defaultOneVal, $ITwo,defaultTwo,defaultTwoVal,$IThree,defaultThree,defaultThreeVal) {
			this.IArray = IArray;
			this.$IOne = $($IOne);
			this.$ITwo = $($ITwo);
			this.$IThree = $($IThree);
			this.defaultOne = defaultOne;
			this.defaultOneVal = defaultOneVal;
			this.defaultTwo = defaultTwo;
			this.defaultTwoVal = defaultTwoVal;
			this.defaultThree = defaultThree;
			this.defaultThreeVal = defaultThreeVal;
			this.oneMap = new this.initMap();
			this.twoMap = new this.initMap();
			this.initOne();
			this.initBind2(this);
			return this;
		},
		initBind2 : function(IBin) {
			this.$IOne.bind('change', function() {
				IBin.initTwo(this.value);
			});
			this.$ITwo.bind('change', function() {
				IBin.initThree(this.value);
			});
		},
		initOne: function() {
			this.$IOne.empty();
			this.$IOne.append('<option value="">' + this.defaultOne+ '</option>');
			if (typeof (this.IArray) == "undefined") {

			} else {
				if (typeof (this.oneMap) == "undefined") {
					this.oneMap = new this.initMap();
				}
				if (typeof (this.twoMap) == "undefined") {
					this.twoMap = new this.initMap();
				}
				if (this.IArray != null) {
					var BinOneLen = this.IArray.length;
					for (var i = 0; i < BinOneLen; i++) {
						var v = this.IArray[i].name;
						if(v==this.defaultOneVal){
							this.$IOne.append('<option value="'+ v+ '" selected>'+ v + '</option>');
						}else{
							this.$IOne.append('<option value="'+ v + '">'+ v + '</option>');
						}
						var twoArray = this.IArray[i].childs;
						this.oneMap.put(this.IArray[i].name,twoArray);
						if (typeof (twoArray) == "undefined") {

						}else{
							if(twoArray!=null){
								var BinTwoLen = twoArray.length;
								for(var j = 0; j < BinTwoLen; j++){
									this.twoMap.put(twoArray[j].name,twoArray[j].childs);
								}
							}
						}
					}
				}
			}
			this.initTwo(this.defaultOneVal);
		},
		initTwo : function(parentCode) {

			this.$ITwo.empty();
			this.$ITwo.append('<option value="">' + this.defaultTwo+ '</option>');
			if (!(typeof (parentCode) == "undefined") || !parentCode == null) {
				var TwoArray = this.oneMap.get(parentCode);
				if (typeof (TwoArray) == "undefined") {

				} else {
					if (TwoArray != null) {
						var BinTwoLen = TwoArray.length;
						for (var i = 0; i < BinTwoLen; i++) {
							var v = TwoArray[i].name;
							if(v==this.defaultTwoVal){
								this.$ITwo.append('<option data-zip="'+TwoArray[i].zip+'" value="'+ v + '" selected>'+ v + '</option>');
							}else{
								this.$ITwo.append('<option data-zip="'+TwoArray[i].zip+'" value="'+ v + '">'+ v + '</option>');
							}
						}
					}
				}
			}
			this.initThree(this.defaultTwoVal);
		},
		initThree : function(parentCode) {
			this.$IThree.empty();
			this.$IThree.append('<option value="">' + this.defaultThree+ '</option>');
			if (!(typeof (parentCode) == "undefined") || !parentCode == null) {
				var ThreeArray = this.twoMap.get(parentCode);
				if (typeof (ThreeArray) == "undefined") {

				} else {
					if (ThreeArray != null) {
						var BinThreeLen = ThreeArray.length;
						for (var i = 0; i < BinThreeLen; i++) {
							var v = ThreeArray[i].name;
							if(v==this.defaultThreeVal){
								this.$IThree.append('<option value="'+ v+ '" selected>'+ v + '</option>');
							}else{
								this.$IThree.append('<option value="'+ v + '">'+ v+ '</option>');
							}
							
						}

					}
				}
			}
		},
		initMap : function() {
			/** 存放键的数组(遍历用到) */
			this.keys = new Array();
			/** 存放数据 */
			this.data = new Object();

			/**
			 * 放入一个键值对
			 * 
			 * @param {String}
			 *            key
			 * @param {Object}
			 *            value
			 */
			this.put = function(key, value) {
				if (this.data[key] == null) {
					this.keys.push(key);
				}
				this.data[key] = value;
			};

			/**
			 * 获取某键对应的值
			 * 
			 * @param {String}
			 *            key
			 * @return {Object} value
			 */
			this.get = function(key) {
				return this.data[key];
			};

			/**
			 * 删除一个键值对
			 * 
			 * @param {String}
			 *            key
			 */
			this.remove = function(key) {
				this.keys.remove(key);
				this.data[key] = null;
			};

			/**
			 * 遍历Map,执行处理函数
			 * 
			 * @param {Function}
			 *            回调函数 function(key,value,index){..}
			 */
			this.each = function(fn) {
				if (typeof fn != 'function') {
					return;
				}
				var len = this.keys.length;
				for (var i = 0; i < len; i++) {
					var k = this.keys[i];
					fn(k, this.data[k], i);
				}
			};

			/**
			 * 获取键值数组(类似<a href="http://lib.csdn.net/base/java"
			 * class='replace_word' title="Java 知识库" target='_blank'
			 * style='color:#df3434; font-weight:bold;'>Java</a>的entrySet())
			 * 
			 * @return 键值对象{key,value}的数组
			 */
			this.entrys = function() {
				var len = this.keys.length;
				var entrys = new Array(len);
				for (var i = 0; i < len; i++) {
					entrys[i] = {
						key : this.keys[i],
						value : this.data[i]
					};
				}
				return entrys;
			};

			/**
			 * 判断Map是否为空
			 */
			this.isEmpty = function() {
				return this.keys.length == 0;
			};

			/**
			 * 获取键值对数量
			 */
			this.size = function() {
				return this.keys.length;
			};

			/**
			 * 重写toString
			 */
			this.toString = function() {
				var s = "{";
				for (var i = 0; i < this.keys.length; i++, s += ',') {
					var k = this.keys[i];
					s += k + "=" + this.data[k];
				}
				s += "}";
				return s;
			};
		}

	};
}
